package com.bnq.logc.utils

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

class DateUtil {
    companion object{
        fun formatDateTime(timeStamp : Long,pattern:String = "yyyy-MM-dd HH:mm:ss.SSS"):String{
            val date = Date(timeStamp)
            val simpleDateFormat = SimpleDateFormat(pattern)
            val dateStr = simpleDateFormat.format(date)
            return dateStr
        }

        fun currentDateTimeString():String = formatDateTime(System.currentTimeMillis())

        fun getCurrentYMString(timeStamp : Long = System.currentTimeMillis()):String = formatDateTime(timeStamp,"yyy-MM")
        fun getCurrentYMDString(timeStamp : Long = System.currentTimeMillis()):String = formatDateTime(timeStamp,"yyyy-MM-dd")

        fun timeToTimestamp(time : String,pattern:String = "yyyy-MM-dd HH:mm:ss"):Long{
            try{
                val simpleDateFormat = SimpleDateFormat(pattern)
                val date = simpleDateFormat.parse(time)
                return date.time
            }catch (e: ParseException){
                return 0
            }
        }

        fun timeToTimestampYMD(time : String):Long{
            try{
                val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
                val date = simpleDateFormat.parse(time)
                return date.time
            }catch (e: ParseException){
                return 0
            }
        }

        fun diff(startTime:Long,endTime:Long):Long = endTime - startTime

        fun getDiffDaysToNow(time:String):Double{
            val t = timeToTimestamp(time,"yyyy-MM-dd")
            if(t != 0L){
                return getDiffDaysToNow(t)
            }
            return 0.0
        }
        fun getDiffDaysToNow(timeStamp : Long):Double{
            val diff = diff(timeStamp,System.currentTimeMillis())
            return diff / (24 * 60 * 60 * 1000.0)
        }
    }
}